<template>
  <div class="bottom-bar">
    <div class="check-all">
      <Btn :isChecked="isSelectAll" @click.native="checkAll" />
      <div class="check-text">全选</div>
    </div>
    <div class="price-all">
      <span>合计：￥{{ totalPrice }}</span>
    </div>
    <div class="go-count">去计算({{ checkedLen }})</div>
  </div>
</template>

<script>
import Btn from "components/content/checkButton/CheckButton";
import { mapGetters } from "vuex";
export default {
  components: {
    Btn,
  },
  computed: {
    ...mapGetters(["getCartList"]),
    totalPrice() {
      return (
        this.getCartList
          .filter((item) => {
            //返回选中的商品
            return item.checked;
          })
          //运算
          .reduce((preValue, item) => {
            return preValue + item.realPrice * item.count;
          }, 0)
          //保留2位小数
          .toFixed(2)
      );
    },
    checkedLen() {
      return this.getCartList.filter((item) => {
        return item.checked;
      }).length;
    },
    isSelectAll() {
      return (
        this.getCartList.length !== 0 &&
        !this.getCartList.some((item) => !item.checked)
      );
    },
  },
  methods: {
    checkAll() {
      if (this.isSelectAll) {
        //全部选中
        this.getCartList.forEach((item) => (item.checked = false));
      } else {
        //部分或全部选中
        this.getCartList.forEach((item) => (item.checked = true));
      }
    },
  },
};
</script>

<style scoped>
.bottom-bar {
  display: flex;
  height: 40px;
  position: relative;
  bottom: 40px;
  align-items: center;
  justify-content: space-between;
  background-color: #eeeeee;
}
.check-all {
  margin-left: 10px;
}
.check-text {
  position: absolute;
  left: 40px;
  top: 12px;
}
.go-count {
  width: 100px;
  height: 40px;
  background: red;
  line-height: 40px;
  text-align: center;
  color: #ffff;
}
</style>